/**
 * 根据登录用户的角色权限，筛选前端路由配置数组中的匹配项
 * @param {*} permissions
 * @param {*} routes
 * @returns
 */
const genPermissionRoutes = (roles, routes) => {
  const result = []
  //   遍历权限数组
  roles.forEach((role) => {
    //
    // 保存当前遍历到的权限路由
    const route = routes.find((route) => route.to === role.menu)
    // 浅拷贝此路由
    const copy = { ...route }
    // 如果当前权限对象有子级，且当前找到的路由有子级，则递归调用函数继续筛选
    if (role.children && route.children) {
      const children = genPermissionRoutes(role.children, route.children)
      copy.children = children
    }
    // 把处理好的路由复制对象加入到 result 中
    result.push(copy)
  })

  return result
}

export default genPermissionRoutes


/*
permissions.forEach((permission) => {
    const route = routes.find((route) => route.path === permission.menu.url)
    const copy = { ...route }
    // 如果当前权限对象有子级，且当前找到的路由有子级，则递归调用函数继续筛选
    if (permission.children && route.children) {
      const children = genPermissionRoutes(permission.children, route.children)
      copy.children = children
    }

    result.push(copy)
  })
*/
